package com.zhiwei.msf.app.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.zhiwei.msf.app.dto.ProjectDto;
import com.zhiwei.msf.app.mapper.DictionaryMapper;
import com.zhiwei.msf.app.mapper.ExtProjectMapper;
import com.zhiwei.msf.app.mapper.ProjectMapper;
import com.zhiwei.msf.app.po.Dictionary;
import com.zhiwei.msf.app.po.DictionaryExample;
import com.zhiwei.msf.app.po.Project;
import com.zhiwei.msf.app.po.ProjectExample;
import com.zhiwei.msf.app.service.ProjectService;
import com.zhiwei.msf.common.constant.DatabaseConstants;
import com.zhiwei.msf.common.dto.PageDataDto;
import com.zhiwei.msf.common.util.SqlUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zhiwei_yang
 * @time 2020-6-10-16:35
 */
@Slf4j
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ExtProjectMapper extProjectMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    public PageDataDto<Project> search(ProjectDto projectDto) {
        ProjectExample projectExample = new ProjectExample();
        ProjectExample.Criteria criteria = projectExample.createCriteria();
        if (!StringUtils.isEmpty(projectDto.getProjectName())) {
            criteria.andProjectNameLike(SqlUtil.getLikeSplit(projectDto.getProjectName()));
        }
        if (!StringUtils.isEmpty(projectDto.getProjectType())) {
            criteria.andProjectTypeEqualTo(projectDto.getProjectType());
        }
        if (!StringUtils.isEmpty(projectDto.getCodeLanguage())) {
            criteria.andCodeLanguageEqualTo(projectDto.getCodeLanguage());
        }
        if (!StringUtils.isEmpty(projectDto.getBuildType())) {
            criteria.andBuildTypeEqualTo(projectDto.getBuildType());
        }
        criteria.andEnableFlagEqualTo(null == projectDto.getEnableFlag() ? DatabaseConstants.ENABLE : projectDto.getEnableFlag());

        PageDataDto<Project> projectPageDataDto = new PageDataDto<>();

        int total = projectMapper.countByExample(projectExample);
        projectPageDataDto.setRowCount(total);
        projectPageDataDto.setPageNow(projectDto.getPageNow());
        projectPageDataDto.setPageCount(total % projectDto.getPageSize() == 0 ? total / projectDto.getPageSize() : total / projectDto.getPageSize() + 1);
        if (0 == total) {
            projectPageDataDto.setData(Collections.emptyList());
            log.info("项目配置搜索数据为空 projectDto ==> {}", JSON.toJSONString(projectDto));
            return projectPageDataDto;
        }
        RowBounds rowBounds = new RowBounds((projectDto.getPageNow() - 1) * projectDto.getPageSize(), projectDto.getPageSize());
        List<Project> projectList = extProjectMapper.selectByExampleByPage(projectExample, rowBounds);

        //格式化项目类型、开发语言、构建类型、部署类型
        Set<String> projectIdSet = new HashSet<>();
        projectIdSet.addAll(projectList.stream().map(Project::getProjectType).collect(Collectors.toSet()));
        projectIdSet.addAll(projectList.stream().map(Project::getCodeLanguage).collect(Collectors.toSet()));
        projectIdSet.addAll(projectList.stream().map(Project::getBuildType).collect(Collectors.toSet()));
        projectIdSet.addAll(projectList.stream().map(Project::getDeployType).collect(Collectors.toSet()));
        DictionaryExample dictionaryExample = new DictionaryExample();
        DictionaryExample.Criteria dictionaryExampleCriteria = dictionaryExample.createCriteria();
        dictionaryExampleCriteria.andUuidIn(new ArrayList<>(projectIdSet));
        dictionaryExampleCriteria.andEnableFlagEqualTo(DatabaseConstants.ENABLE);
        List<Dictionary> dictionaries = dictionaryMapper.selectByExample(dictionaryExample);
        Map<String, Dictionary> dictionaryMap = dictionaries.stream().collect(Collectors.toMap(Dictionary::getUuid, obj -> obj));

        projectList.forEach(project -> {
            project.setProjectType(null == dictionaryMap.get(project.getProjectType()) ?
                    dictionaryMap.get(project.getProjectType()).getValue() : "");
            project.setCodeLanguage(null == dictionaryMap.get(project.getCodeLanguage()) ?
                    dictionaryMap.get(project.getCodeLanguage()).getValue() : "");
            project.setBuildType(null == dictionaryMap.get(project.getBuildType()) ?
                    dictionaryMap.get(project.getBuildType()).getValue() : "");
            project.setDeployType(null == dictionaryMap.get(project.getDeployType()) ?
                    dictionaryMap.get(project.getDeployType()).getValue() : "");
        });

        projectPageDataDto.setData(projectList);
        log.info("项目配置搜索数据 projectDto:{} projectPageDataDto ==> {}", JSON.toJSONString(projectDto), JSON.toJSONString(projectPageDataDto));
        return projectPageDataDto;
    }
}